package com.chen.dao.impl;



import com.chen.dao.DeliveryDao;
import com.chen.entity.Delivery;
import com.chen.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class DeliveryDaoImpl implements DeliveryDao {
    Connection conn=DBUtil.getConnection();
    QueryRunner qr=new QueryRunner();
    @Override
    public int save(Delivery obj) {
        String sql="INSERT INTO  tb_delivery(delivery_id,user_id,delivery_name,delivery_phone,delivery_address) VALUES (?,?,?,?,?)";
        Object[] paras={obj.getDelivery_id(),obj.getUser_id(),obj.getDelivery_name(),obj.getDelivery_phone(),obj.getDelivery_address()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Delivery obj) {
        String sql="UPDATE  tb_delivery SET  user_id=?,delivery_name=?,delivery_phone=?,delivery_address=? WHERE delivery_id=? ";
        Object[] paras={obj.getUser_id(),obj.getDelivery_name(),obj.getDelivery_phone(),obj.getDelivery_address(),obj.getDelivery_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql="DELETE FROM tb_delivery WHERE delivery_id  = ? ";
        Object[] paras={id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Delivery findById(Object key) {
        String sql="SELECT * FROM tb_delivery where delivery_id  = ?";
        Object[] paras={key};
        try {
            Delivery delivery=qr.query(conn,sql,paras,new BeanHandler<Delivery>(Delivery.class));
            return delivery;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Delivery> findAll() {
        String sql="SELECT * FROM tb_delivery";
        Object[] paras={};
        try {
            List<Delivery> deliveryList=qr.query(conn,sql,paras,new BeanListHandler<Delivery>(Delivery.class));
            return deliveryList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Delivery> findSearch(String keyword) {
        return null;
    }

    @Override
    public List<Delivery> findPage(int page, int pagesize) {
        String sql="SELECT * FROM tb_delivery limit ?,?";
        Object[] paras={(page-1)*pagesize,pagesize};
        try {
            List<Delivery> deliveryList=qr.query(conn,sql,paras,new BeanListHandler<Delivery>(Delivery.class));
            return deliveryList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql="select count(*) from tb_delivery";
        Object[] paras={};
        try {
            long nums=qr.query(conn,sql,paras,new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
